<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="设备id" prop="devId">
      <el-input v-model="dataForm.devId" placeholder="设备id"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="descr">
      <el-input v-model="dataForm.descr" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="状态(0未处理,1开始处理,2处理完毕)" prop="state">
      <el-input v-model="dataForm.state" placeholder="状态(0未处理,1开始处理,2处理完毕)"></el-input>
    </el-form-item>
    <el-form-item label="报修时间" prop="sendTime">
      <el-input v-model="dataForm.sendTime" placeholder="报修时间"></el-input>
    </el-form-item>
    <el-form-item label="处理人userId" prop="operatorUserId">
      <el-input v-model="dataForm.operatorUserId" placeholder="处理人userId"></el-input>
    </el-form-item>
    <el-form-item label="处理人名称(冗余)" prop="operatorName">
      <el-input v-model="dataForm.operatorName" placeholder="处理人名称(冗余)"></el-input>
    </el-form-item>
    <el-form-item label="处理开始时间" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="处理开始时间"></el-input>
    </el-form-item>
    <el-form-item label="处理结束时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="处理结束时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: '',
          devId: '',
          descr: '',
          state: '',
          sendTime: '',
          operatorUserId: '',
          operatorName: '',
          startTime: '',
          endTime: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          devId: [
            { required: true, message: '设备id不能为空', trigger: 'blur' }
          ],
          descr: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '状态(0未处理,1开始处理,2处理完毕)不能为空', trigger: 'blur' }
          ],
          sendTime: [
            { required: true, message: '报修时间不能为空', trigger: 'blur' }
          ],
          operatorUserId: [
            { required: true, message: '处理人userId不能为空', trigger: 'blur' }
          ],
          operatorName: [
            { required: true, message: '处理人名称(冗余)不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '处理开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '处理结束时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/zhiyao/zyrepair/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.data.zyRepair
                // this.dataForm.userId = data.zyrepair.userId
                // this.dataForm.devId = data.zyrepair.devId
                // this.dataForm.descr = data.zyrepair.descr
                // this.dataForm.state = data.zyrepair.state
                // this.dataForm.sendTime = data.zyrepair.sendTime
                // this.dataForm.operatorUserId = data.zyrepair.operatorUserId
                // this.dataForm.operatorName = data.zyrepair.operatorName
                // this.dataForm.startTime = data.zyrepair.startTime
                // this.dataForm.endTime = data.zyrepair.endTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/zhiyao/zyrepair/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                zyRepair: this.dataForm
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  // duration: 1500,
                  duration: 500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
